<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>AUI快速完成布局</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
</head>

<body>

    <div id="list">

    </div>
    <div style="text-align:center;margin:0.625rem;display:none;background:#fff;padding:1.25rem;" id="default-1">
        <img src="./img/muyou.png" style="display:inline;">
        <p><span style="font-size:1.25rem;">亲,</span>&nbsp;&nbsp;&nbsp;&nbsp;您当前木有对应的试用品</p>
    </div>

</body>
<script type="text/template" id="template">
    {{~it:value:index}}
    <ul class="aui-list aui-media-list" style="margin-bottom:0.225rem;" onclick="fnApplyIng(this)">
        <li class="aui-list-item">
            <div class="aui-list-item-inner">
                <div class="aui-row">

                    <div class="aui-col-xs-12 time5">
                        待商家审核资格倒计时：<span style="color:red"></span>
                        <input type="hidden" value="{{=value.wait_audit_time}}" class="timeNum">
                    </div>
                </div>
            </div>
        </li>
        <li class="aui-list-item">
            <div class="aui-media-list-item-inner">
                <div class="aui-list-item-media" style="width:8rem;height:8rem">
                    <img src="{{= fnSiteUrl(value.thumb)}}" style="width:8rem;height:8rem">
                </div>
                <div class="aui-list-item-inner" style="width:10rem">
                    <div class="aui-list-item-text">
                        <div class="aui-list-item-title" style="white-space: nowrap;text-overflow:ellipsis;overflow:hidden;">{{= value.title}}</div>
                    </div>
                    <div class="aui-list-item-text">
                        <p>Id:{{= value.goods_id}}</p>
                        <input type="hidden" class="orderid" value="{{= value.oid}}">
                    </div>
                    <div class="aui-list-item-text">
                        <p>申请: <span>{{=userDate(value.create_time)}}</span></p>
                    </div>
                    <div class="aui-list-item-text">
                        <p>试用价: <span style="color:red">{{= value.agoods_price }}</span></p>
                        <p>返：<span style="color:red">{{= value.agoods_price }}</span></p>

                    </div>
                    <div class="aui-list-item-text">
                        <p>旺号:{{= value.account }}</p>


                    </div>
                    <div class="aui-list-item-text">
                        <p>等级：<img src="{{= fnSiteUrl(value.account_level)}}" alt="" style="display:inline;"></p>
                    </div>
                    <div class="aui-list-item-text">
                        <p>状态:申请中</p>
                        <p style="color:#fff;background:#ff4285;padding:0.125rem;border-radius:5px;">点击操作</p>
                    </div>
                </div>
            </div>
        </li>
    </ul>
    {{~}}

</script>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="./conf/config.js"></script>
<script type="text/javascript" src="./js/common.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script type="text/javascript">
    apiready = function() {
        fts = "";
        fnGetData();
        initRefresh();
    };
    //下拉刷新，松开刷新
    function initRefresh() {
        api.setRefreshHeaderInfo({
            visible: true,
            loadingImg: 'widget://image/refresh.png',
            bgColor: '#ccc',
            textColor: '#fff',
            textDown: '下拉刷新...',
            textUp: '松开刷新...',
            showTime: true
        }, function(ret, err) {
            fnGetGoodsList();
        });
    }


    function fnUpdateList(ret) {

        var list = $api.byId('list'); //获取节点
        //var str={"array":["banana","apple","orange"]};
        // 1. 编译模板函数
        var tempFn = doT.template($api.byId('template').innerHTML); //找到模板多次使用
        // 2. 多次使用模板函数
        var resultText = tempFn(ret.data);
        $api.html(list, "");
        $api.append(list, resultText);
        fts = setInterval("fnclock2()", 1000);
        //$api.html(list, resultText);
    }

    function fnUpdateListDefault() {
        $("#default-1").show();
    }

    function fnGetData() {
        clearTimeout(fts);
        var userInfo = $api.getStorage('userInfo');
        //获取数据app中存入的数据然后分配到模板中；
        if(userInfo){
          var random = userInfo.random;
          var userid = userInfo.userid;
        }else{
          var random = "";
          var userid ="";
        }

        api.ajax({
            url: SITE_URL + 'index.php?m=api&c=appnew&a=order_apply',
            method: 'post',
            data: {
                values: {
                    userid: userid,
                    random: random,
                    trial: 'trial',
                    state: '1'
                },

            }
        }, function(ret, err) {
            switch (ret.status) {
                case 0:
                //alert(111);
                    alert(ret.msg);
                    api.refreshHeaderLoadDone(); //下拉刷新隐藏
                    break;
                case 1:
                    fnUpdateList(ret);
                    api.refreshHeaderLoadDone(); //下拉刷新隐藏
                    break;
                case 2:

                  //alert(ret.msg);
                    api.openWin({
                        name: 'login',
                        url: './login.html',
                        pageParam: {
                            name: 'test'
                        }
                    });

                    //然后关闭当前网页，跳转到另一个网页；
                    break;
                case 5:
                    fnUpdateListDefault();
                    api.refreshHeaderLoadDone(); //下拉刷新隐藏
                    break;
            }



        });

    }

    function clock(stringTime) {
        //alert(stringTime);
        var timestamp2 = Date.parse(new Date(stringTime));
        //alert(timestamp2);
        return timestamp2;
        //alert(timestamp2);

        // var endTime = new Date(timeVal*1000);
        // var nowTime = new Date();
        // var second = parseInt((endTime.getTime() - nowTime.getTime()) / 1000);
        // var d = parseInt(second / 60 / 60 / 24); //计算剩余的天数
        // var h = parseInt(second / 60 / 60 % 24); //计算剩余的小时
        // var m = parseInt(second / 60 % 60); //计算剩余的分钟
        // var s = parseInt(second % 60); //计算剩余的秒数
        // var html = d + "天 " + h + "小时 " + m + "分钟 " + s + "秒";
        // $("#time-last").html(html);
        // $("#time-last-2").val(second);

        //});

    }

    //oid
    function fnApplyIng(that) {
        //获取订单Id
        var orderId = $(that).find(".orderid").val();
        api.actionSheet({
            title: '操作',
            buttons: ['操作记录', '撤销申请']
        }, function(ret, err) {
            var index = ret.buttonIndex;
            switch (index) {
                case 1:
                    fnOperateLogWin(orderId);
                    break;
                case 2:
                    fnRepealApply(orderId);
                    break;

            }

        });
    }

    function fnRepealApply(orderId) {
        api.confirm({
            title: '提示：',
            msg: "您确定撤销申请吗？",
            buttons: ['确定', '取消']
        }, function(ret, err) {
            var index = ret.buttonIndex;
            if (index == 1) {
                var userInfo = $api.getStorage('userInfo');
                //获取数据app中存入的数据然后分配到模板中；
                var random = userInfo.random;
                var userid = userInfo.userid;
                api.ajax({
                    url: SITE_URL + 'index.php?m=api&c=appnew&a=close_order_sn',
                    method: 'POST',
                    data: {
                        values: {
                            "userid": userid,
                            "random": random,
                            "order_id": orderId,
                        }
                    }
                }, function(ret, err) {
                    if (ret.status == 1) {
                        fnGetData();
                    }
                });

            }
        });

    };


    function fnOperateLogWin(orderId) {
        fnGetDataLog(orderId);
    }

    function fnGetDataLog(orderId) {
        var userInfo = $api.getStorage('userInfo');
        //获取数据app中存入的数据然后分配到模板中；
        var random = userInfo.random;
        var userid = userInfo.userid;
        api.ajax({
            url: SITE_URL + 'index.php?m=api&c=appnew&a=view_log',
            method: 'POST',
            data: {
                values: {
                    "userid": userid,
                    "random": random,
                    "orderid": orderId,
                }
            }
        }, function(ret, err) {
            //alert(JSON.stringify(ret));
            if (ret.status == 1) {
                api.openWin({
                    name: "task_operate_log",
                    url: './task_operate_log.html',
                    pageParam: {
                        orderId: orderId
                    }
                });
            } else {
                alert(ret.msg);
            }

        });
    }


    //下拉刷新，松开刷新
    function initRefresh() {
        api.setRefreshHeaderInfo({
            visible: true,
            loadingImg: 'widget://image/refresh.png',
            bgColor: '#ccc',
            textColor: '#fff',
            textDown: '下拉刷新...',
            textUp: '松开刷新...',
            showTime: true
        }, function(ret, err) {
            //fnGetGoodsList();
            fnGetData();
        });
    }
</script>

</html>
